/*
 * @Author: qiuziz
 * @Date: 2017-08-15 10:24:59
 * */

import React from 'react';
import { Router, Route, Redirect, hashHistory, IndexRoute } from 'react-router';

import Home from '../container/home';
import EmployeeInfoEdit from '../container/employee/employeeInfoEdit';
import LogList from '../container/worklog/loglist';
import AddProject from '../container/project/addProject';
import Workload from '../container/project/workload';
import Login from '../container/login';
import ReactCSSTransitionGroup from "react-addons-css-transition-group";
import BrowserUtils from './history';
import { Resource } from './resource';
import LocalStorage from './LocalStorage';

class App extends React.Component {

	
  render () {
    return (
		<div className="container">
				<ReactCSSTransitionGroup
						component="div"
						transitionName={BrowserUtils.direction}
						transitionEnterTimeout={300}
						transitionLeaveTimeout={300}>
						<div key={this.props.location.pathname}
							style={{position:"absolute", width: "100%"}}>
							{
									this.props.children
							}
					</div>
				</ReactCSSTransitionGroup>
		</div>
    )
  }
}

const isLogin = (nextState, replace, callback) => {
	const user = LocalStorage.getItem('user');
	if (user) {
		Resource.login.get({name: user.userName, password: user.passWord}).then(res => {
			if (res && res.result === 1) {
			} else {
				LocalStorage.removeItem('user');
			}
		}).catch(err => {
			console.log(err);
		})
		callback();
	} else {
		replace({pathname: '/login'});
	}
}


export default (
	<Router history={hashHistory}>
		<Route>
				<Route path="/" component={App} onEnter={(...args) => isLogin(...args)}>
					{
						<IndexRoute component={Home}/>
					}
					<Route path="/home" component={Home} />
					<Route path="/logList" component={LogList} />
					<Route path="/addProject" component={AddProject} />
					<Route path="/employeeInfoEdit" component={EmployeeInfoEdit} />
					<Route path="/workload" component={Workload} />
				</Route>
				<Route path="/login" component={Login} />
				<Redirect from='*' to='/' />
		</Route>
	</Router>
);
